package com.zyp.test.test;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

/**
 * @author zyp
 *         2017/12/22
 */

public class Main5Activity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        ImageView imageView = (ImageView) findViewById(R.id.iv_icon);
        BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
        Bitmap bitmap1 = Bitmap.createBitmap(400,400, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawColor(Color.RED);
       Bitmap bitmap = drawable.getBitmap();
        imageView.setImageBitmap(bitmap1);
        imageView.setImageBitmap(handleImageEffect(bitmap,90));
    }
    public static Bitmap handleImageEffect(Bitmap bitmap,float hue){
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint();
        ColorMatrix hueRMatrix = new ColorMatrix();
        printLog("原始数组",hueRMatrix.getArray());
        hueRMatrix.setRotate(0,hue);
        printLog("绕R旋转",hueRMatrix.getArray());
        /*hueRMatrix.reset();
        hueRMatrix.setRotate(1,hue);
        printLog("绕G旋转",hueRMatrix.getArray());
        hueRMatrix.reset();
        hueRMatrix.setRotate(2,hue);
        printLog("绕B旋转",hueRMatrix.getArray());*/
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(1f);
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(1,1,1,1);
        ColorMatrix colorMatrix = new ColorMatrix();
        //colorMatrix.postConcat(saturationMatrix);
        colorMatrix.postConcat(lumMatrix);
        //colorMatrix.postConcat(hueRMatrix);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap,0,0,paint);
        return bmp;
    }
    
    public static void printLog(String tag,float[] values){
        
        StringBuilder stringBuilder = new StringBuilder();
        if(tag != null){
            stringBuilder.append(tag).append("\n");
        }
        for (int i = 0; i < values.length ; i++){
            float value = values[i];
            if(Math.abs(value) > 0 && Math.abs(value) < 0.0001){
                value = Math.round(value);
            }
            if (value >= 0){
                stringBuilder.append(" ");
            }
            stringBuilder.append(value).append(" ");
            if((i+1)%5 == 0){
                stringBuilder.append("\n");
            }
        }
        Log.d("ColorMatrix",stringBuilder.toString());
    }
}
